<?php
/**
 * Created by PhpStorm.
 * User: wangkxin@foxmail.com
 * Date: 2022/4/14
 * Time: 23:15
 */

namespace app\api\controller;
use app\api\model\User as UserModel;
use app\api\server\Token as TokenServer;
use app\Request;

class Login
{
    public function login(Request $request)
    {
        // return '登录';
        //获取code码
        $code = $request->get('code');
        //获取微信授权url
        $url = sprintf(config('wx.wxLoginUrl'),config('wx.AppID'),config('wx.AppSecret'),$code);
        //获取openid
        // $data =  curlGet($url);
        $data = ['openid'=>'web111'];
        //进行查询数据库里面是否有该用户，如果没有，则进行新增
        $user = UserModel::where('openid',$data['openid'])->find();
        //如果没有用户进行创建
        if (empty($user)){
            $user = UserModel::create([
                'openid'=>$data['openid']
            ]);
        }
        //生成token,保存用户登录状态
        $token = (new TokenServer())->generateToken($user->id);

        return json(['token'=>$token,'error_code'=>0,'msg'=>'登录成功']);
    }









}